package com.qf.lja.servlet;

import com.qf.lja.dao.UserDao;
import com.qf.lja.enity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet(urlPatterns = "/updateInfo")
public class UpdateInfoServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //utf-8
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String name = req.getParameter("a_name");
        String phone = req.getParameter("a_phone");
        String email = req.getParameter("a_email");

        UserDao userDao = new UserDao();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        userDao.updateInfo(user.getUserId(), name, phone, email);

        //更新user信息
        user = userDao.findByUserId(user.getUserId());
        session.setAttribute("user", user);
        req.setAttribute("msg", "用户信息更新成功 !");
        //转发
        req.getRequestDispatcher("/info.jsp").forward(req, resp);

    }
}
